import type {
  ActionType,
  ProColumns,
  ProDescriptionsItemProps,
} from '@ant-design/pro-components';
import {
  FooterToolbar,
  PageContainer,
  ProDescriptions,
  ProTable,
} from '@ant-design/pro-components';
import { Button, Drawer, Input, message, Popconfirm } from 'antd';
import { FormattedMessage, useIntl, useRequest } from '@umijs/max';
import React, { useCallback, useRef, useState } from 'react';
import { removeCategory, categoryTree } from '@/services/ant-design-pro/api';
import CreateForm from './components/CreateForm';
const Category: React.FC = () => {
  const actionRef = useRef<ActionType | null>(null);
  const [categoryData, setCategoryData] = useState([])
  const [messageApi, contextHolder] = message.useMessage();

  const { run, loading } = useRequest(removeCategory, {
    manual: true,
    onSuccess: () => {
      messageApi.success('删除成功');
      actionRef.current?.reload?.();
    },
    onError: () => {
      messageApi.error('删除失败');
    },
  });
  /**
   * @en-US International configuration
   * @zh-CN 国际化配置
   * */
  const intl = useIntl();


  const columns: ProColumns<API.RuleListItem>[] = [
    {
      title: '分类名称',
      dataIndex: 'name',
    },
    
    {
      title: '状态',
      dataIndex: 'status',
      hideInForm: true,
      valueEnum: {
        1: {
          text: '上架',
          status: 'Processing',
        },
        2: {
          text: '下架',
          status: 'Default',
        },
      },
    },
    {
      title: '创建时间',
      sorter: true,
      dataIndex: 'create_time',
      valueType: 'dateTime',
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record: any) => [
        <CreateForm
          key="config"
          reload={actionRef.current?.reload}
          data={categoryData}
          values={record}
        />,
        <Popconfirm key="delete" title="确定删除吗？" okText="是" cancelText="否" onConfirm={() => run(record.id as number)}>
          <a>删除</a>
        </Popconfirm>
      ],
    },
  ];
  return (
    <PageContainer>
      <ProTable<API.RuleListItem>
        headerTitle="分类列表"
        actionRef={actionRef}
        rowKey="id"
        search={{
          labelWidth: 120,
        }}
        toolBarRender={() => [
          <CreateForm key="create" reload={actionRef.current?.reload} data={categoryData} />,
        ]}
        request={categoryTree}
        postData={(data: any) => {
          setCategoryData(data)
          return data
        }}
        params={{ parentId: 0 }}
        columns={columns}
      />
    </PageContainer>
  )
}

export default Category;